<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 16/6/16
 * Time: 下午4:08
 */

$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

DB::connection()->disableQueryLog(); 

$report = array();
$mail = array(
    'ahlyt@anhuibj.com',
);

// 三月份到现在的数据
// $_date=array("2017-04-27","2017-09-01");//清理的时间
// $cur_date=(strtotime($_date[1]) - strtotime($_date[0])) / (86400*1);
// for($i=1;$i<=$cur_date;$i++){
//     $begin = date("Y-m-d 00:00:00",strtotime($_date[0]." +".(($i-1)*1)." day"));
//     $end_time = date("Y-m-d 23:59:59",strtotime($_date[0]." +".($i*1-1)." day"));
//     $report=get_work_hour_product($begin,$end_time);
//     foreach ($report as $_report) {
//         $str="'".$_report['order_id']."','".$_report['order_type']."','".$_report['pay_time']."','".$_report['shop_name']."','".$_report['counter_amount']."','".$_report['supply_amount']."','".$_report['car_no']."','".$_report['product_name']."','".$_report['level_1_name']."/".$_report['level_2_name']."/".$_report['level_3_name']."','".$_report['user_type']."','".$_report['user_name']."','".$_report['car_model']."','".$_report['car_model_level']."'\n";    
//         $str=iconv("utf-8",'gbk//IGNORE',$str);

//         // file_put_contents("D:\\order\\".date("Y-m-d",strtotime($_date[0]." +".(($i-1)*1)." day")).".csv",$str, FILE_APPEND);
//         file_put_contents(public_path()."/app/order/".date("Y-m-d",strtotime($_date[0]." +".(($i-1)*1)." day")).".csv",$str, FILE_APPEND);
//     }

//     Log::info($begin.'-'.$end_time.'订单数据完毕');
//     echo $begin.'-'.$end_time.'订单数据完毕';
// }

//每天定时发送的
$begin=date("Y-m-d 00:00:00",time()-86400);
$end_time=date("Y-m-d 23:59:59",time()-86400);
$report=get_work_hour_product($begin,$end_time);


$report_data = array(
    'mail' => $mail,
    'name' => $begin.'订单数据',
    'date' => $begin,
    'data' => $report
);
Mail::send('orderreport',$report_data, function($message) use ($report_data){
    $message->to($report_data['mail'], $report_data['name'])->subject($report_data['name']);
});

/**
 * @Author:      lyt
 * @DateTime:    2016-06-01 21:10:23
 * @Description: 结算的产品包的统计
 */
function get_work_hour_product($begin_date,$end_date){
    $work_hour_report = array();
    $order = Orders::whereIn('status',array(1018,1017,1217,1317,1417))
        ->whereBetween('pay_time',array($begin_date,$end_date))
        // ->where('shop_id',$shop_id)
        ->where('place',2)
        ->get();    
    foreach ($order as $_order){
        $shop=MaintenanceShops::find($_order->shop_id);
        $user=Users::find($_order->user_id);
        $csi=DataCarStandardInfo::where("id",$_order->car_model_id)->select("brand_name","series_name","model_year","model_name","model_level")->first();
        $order_product = OrderProduct::where('order_id',$_order->order_id)
            ->where('status',1)
            ->get();
        foreach ($order_product as $_order_product){
            if($_order_product->product_type=='enterprise'){//说明是 企业报
                $opep=OrderProductEsPackage::where("order_product_id",$_order_product->id)->get();
                foreach ($opep as $_opep){
                    $supply_amount=0;
                    $service_amount=0;
                    $propack=ProductPackage::find($_opep->package_id);
                    //获取标准的工时成本
                    $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_esitem` where order_product_package_id=".$_opep->id." and type=2");
                    $service_amount=$count[0]->amount;
                    //获取耗材成本
                    $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='enterprise' and product_item_id in(SELECT id FROM `order_product_esitem` where order_product_package_id=".$_opep->id." and type=1)");
                    foreach ($mot_ids as $_mot) {
                        $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                        if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                            continue;
                        }
                        $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();

                        $temp_amount=0;
                        foreach ($before_return_ids as $bri) {
                            $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                            $temp_amount+= $return_amount[0]->amount;
                        }
                        $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                        $supply_amount+=($supply_count[0]->amount+$temp_amount);
                    }
                    if(!is_null($propack)){
                        $service_classification = Report::get_service_classification($propack->classify);    
                        $_work_hour_report_1 = array(
                            'pay_time' => $_order->pay_time,                                                    //支付时间
                            'shop_name' => isset($shop)?$shop->name:"",                                         //门店名字          
                            'shop_id' => $_order->shop_id,                                                      //门店id     
                            'car_no' => $_order->car_no,                                                        //车牌号    
                            'user_name' => $user->real_name,                                                    //用户名
                            'user_type' => $user->type==1?"个人":"企业",                                        //用户类型
                            'car_model' => isset($csi)?$csi->brand_name." ".$csi->series_name." "
                            .$csi->model_year." ".$csi->model_name:"",                                          //车型                      
                            'car_model_level' => $csi->model_level,                                             //车辆级别
                            'order_id' => $_order->order_id,                                                    //订单号
                            'product_name' =>$_order_product->product_name.' '.$propack->name,                  //项目名字 
                            'product_id' => $propack->product_id,                                               //项目ID
                            'product_type' => 0,                                                                //0是标准 1是非标
                            'product_amount' => $_opep->amount,                                                 //原价
                            'product_discount' => 0,                                                            //优惠金额
                            'counter_amount' => $_opep->counter_amount,                                         //实际折后价格
                            'supply_amount' => $supply_amount,                                                  //耗材成本
                            'service_amount' => $service_amount,                                                //服务价格
                            'level_1_id' => $service_classification['level_1_id'],                              //分类
                            'level_1_name' => $service_classification['level_1_name'],          
                            'level_2_id' => $service_classification['level_2_id'],
                            'level_2_name' => $service_classification['level_2_name'],
                            'level_3_id' => $service_classification['level_3_id'],
                            'level_3_name' => $service_classification['level_3_name'],
                            'order_type' => 1,                                                                  //订单类型  1 订单2 洗车单
                        );
                        array_push($work_hour_report,$_work_hour_report_1);
                    }
                }
            }elseif($_order_product->product_type=='activity'){//说明是 活动包
                $opep=OrderProductAcPackage::where("order_product_id",$_order_product->id)->get();
                foreach ($opep as $_opep){
                    $supply_amount=0;
                    $service_amount=0;
                    $propack=ProductPackage::find($_opep->package_id);
                    //获取标准的工时成本
                    $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_acitem` where order_product_package_id=".$_opep->id." and type=2");
                    $service_amount=$count[0]->amount;
                    //获取耗材成本
                    $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='activity' and product_item_id in(SELECT id FROM `order_product_acitem` where order_product_package_id=".$_opep->id." and type=1)");
                    foreach ($mot_ids as $_mot) {
                        $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                        if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                            continue;
                        }
                        $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();

                        $temp_amount=0;
                        foreach ($before_return_ids as $bri) {
                            $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                            $temp_amount+= $return_amount[0]->amount;
                        }


                        $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                        $supply_amount+=($supply_count[0]->amount+$temp_amount);
                    }
                    if(!is_null($propack)){
                        $service_classification = Report::get_service_classification($propack->classify);    
                        $_work_hour_report_1 = array(
                            'pay_time' => $_order->pay_time,                                                    //支付时间
                            'shop_name' => isset($shop)?$shop->name:"",                                         //门店名字          
                            'shop_id' => $_order->shop_id,                                                      //门店id     
                            'car_no' => $_order->car_no,                                                        //车牌号    
                            'user_name' => $user->real_name,                                                    //用户名
                            'user_type' => $user->type==1?"个人":"企业",                                        //用户类型
                            'car_model' => isset($csi)?$csi->brand_name." ".$csi->series_name." "
                            .$csi->model_year." ".$csi->model_name:"",                                          //车型                      
                            'car_model_level' => $csi->model_level,                                             //车辆级别
                            'order_id' => $_order->order_id,                                                    //订单号
                            'product_name' =>$_order_product->product_name.' '.$propack->name,                  //项目名字 
                            'product_id' => $propack->product_id,                                               //项目ID
                            'product_type' => 0,                                                                //0是标准 1是非标
                            'product_amount' => $_opep->amount,                                                 //原价
                            'product_discount' => 0,                                                            //优惠金额
                            'counter_amount' => $_opep->counter_amount,                                         //实际折后价格
                            'supply_amount' => $supply_amount,                                                  //耗材成本
                            'service_amount' => $service_amount,                                                //服务价格
                            'level_1_id' => $service_classification['level_1_id'],                              //分类
                            'level_1_name' => $service_classification['level_1_name'],          
                            'level_2_id' => $service_classification['level_2_id'],
                            'level_2_name' => $service_classification['level_2_name'],
                            'level_3_id' => $service_classification['level_3_id'],
                            'level_3_name' => $service_classification['level_3_name'],
                            'order_type' => 1,                                                                  //订单类型  1 订单2 洗车单
                        );
                        array_push($work_hour_report,$_work_hour_report_1);
                    }
                }
            }else{
                $supply_amount=0;
                $service_amount=0;
                if($_order_product->product_type == 'base'){
                    $propack=ProductPackage::find($_order_product->product_id);
                    //获取标准的工时成本
                    $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_base_item` where order_product_package_id=".$_order_product->id." and type=2");
                    $service_amount=$count[0]->amount;
                    //获取耗材成本
                    $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='base' and product_item_id in(SELECT id FROM `order_product_base_item` where order_product_package_id=".$_order_product->id." and type=1)");
                    foreach ($mot_ids as $_mot) {
                        $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                        if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                            continue;
                        }
                        $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                        $temp_amount=0;
                        foreach ($before_return_ids as $bri) {
                            $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                            $temp_amount+= $return_amount[0]->amount;
                        }


                        $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                        $supply_amount+=($supply_count[0]->amount+$temp_amount);
                    }
                }elseif($_order_product->product_type == 'non_standard'){
                    $propack=Service::find($_order_product->product_id);
                    //获取非标的工时成本
                    $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_non_standard` where order_product_id=".$_order_product->id." and service_type=2 and status=1");
                    $service_amount=$count[0]->amount;
                    //获取耗材成本
                    $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='non_standard' and product_item_id in(SELECT id FROM `order_product_non_standard` where order_product_id=".$_order_product->id." and service_type=1)");
                    foreach ($mot_ids as $_mot) {
                        $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                        if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                            continue;
                        }
                        $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                        $temp_amount=0;
                        foreach ($before_return_ids as $bri) {
                            $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                            $temp_amount+= $return_amount[0]->amount;
                        }

                        $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                        $supply_amount+=($supply_count[0]->amount+$temp_amount);
                    }
                }elseif($_order_product->product_type == 'service_card'){
                    $propack=ProductPackage::find($_order_product->product_id);
                    //获取标准的工时成本
                    $count=DB::select("SELECT sum(counter_amount) as amount FROM `order_product_sc_item` where order_product_id=".$_order_product->id." and type=2");
                    $service_amount=$count[0]->amount;
                    //获取耗材成本
                    $mot_ids=DB::select("select * from maintenance_out_order where status in(2,3) and  product_type='base' and product_item_id in(SELECT id FROM `order_product_base_item` where order_product_package_id=".$_order_product->id." and type=1)");
                    foreach ($mot_ids as $_mot) {
                        $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->sum("number");
                        if($before_return==0&&$_mot->status==3){//如果是 这个出库批次是完成订单后退货 完毕的
                            continue;
                        }
                        $before_return_ids=MaintenanceReturnOrder::where("source",'return_order')->where("out_order_id",$_mot->id)->where("status",1)->select("id")->get();
                        $temp_amount=0;
                        foreach ($before_return_ids as $bri) {
                            $return_amount=DB::select("select sum(msi.number*moi.tax_purchase_price) as amount from maintenance_storage_item msi LEFT JOIN maintenance_out_item moi on moi.id=msi.out_id where msi.return_order_id = (".$bri->id.")");
                            $temp_amount+= $return_amount[0]->amount;
                        }


                        $supply_count=DB::select("select sum((number-return_numer)*tax_purchase_price) as amount from maintenance_out_item where order_out_id=".$_mot->id." and status=1");
                        $supply_amount+=($supply_count[0]->amount+$temp_amount);
                    }
                }else{
                    $propack = null;
                    continue;
                }

                if(!is_null($propack)){
                    // $service_classification = Report::get_service_classification($propack->classify);
                    // $_work_hour_report = array(
                    //     'order_id' => $_order->order_id, //派工单号
                    //     'product_name' => $propack->name,   //客户名称
                    //     'product_id' => $propack->id,   //客户名称
                    //     'product_type' => $_order_product->product_type == 'non_standard'?1:0,   //服务包是标准还是非标  0是标准 1是非标
                    //     'product_amount' => $_order_product->amount,
                    //     'product_discount' => $_order_product->discount,
                    //     'counter_amount' => $_order_product->counter_amount,
                    //     'supply_amount' => $supply_amount,
                    //     'service_amount' => $service_amount,
                    //     'level_1_id' => $service_classification['level_1_id'],
                    //     'level_1_name' => $service_classification['level_1_name'],
                    //     'level_2_id' => $service_classification['level_2_id'],
                    //     'level_2_name' => $service_classification['level_2_name'],
                    //     'level_3_id' => $service_classification['level_3_id'],
                    //     'level_3_name' => $service_classification['level_3_name'],
                    //     'reteurn_type' => 0,
                    // );
                    // array_push($work_hour_report,$_work_hour_report);

                    $service_classification = Report::get_service_classification($propack->classify);    
                        $_work_hour_report_1 = array(
                            'pay_time' => $_order->pay_time,                                                    //支付时间
                            'shop_name' => isset($shop)?$shop->name:"",                                         //门店名字          
                            'shop_id' => $_order->shop_id,                                                      //门店id     
                            'car_no' => $_order->car_no,                                                        //车牌号    
                            'user_name' => $user->real_name,                                                    //用户名
                            'user_type' => $user->type==1?"个人":"企业",                                        //用户类型
                            'car_model' => isset($csi)?$csi->brand_name." ".$csi->series_name." "
                            .$csi->model_year." ".$csi->model_name:"",                                          //车型                      
                            'car_model_level' => isset($csi)?$csi->model_level:"",                                             //车辆级别
                            'order_id' => $_order->order_id,                                                    //订单号
                            'product_name' =>$_order_product->product_name.' '.$propack->name,                  //项目名字 
                            'product_id' => $propack->product_id,                                               //项目ID
                            'product_type' =>$_order_product->product_type=='non_standard'?1:0,                 //0是标准 1是非标
                            'product_amount' => $_order_product->amount,                                        //原价
                            'product_discount' => $_order_product->discount,                                    //优惠金额
                            'counter_amount' => $_order_product->counter_amount,                                //实际折后价格
                            'supply_amount' => $supply_amount,                                                  //耗材成本
                            'service_amount' => $service_amount,                                                //服务价格
                            'level_1_id' => $service_classification['level_1_id'],                              //分类
                            'level_1_name' => $service_classification['level_1_name'],          
                            'level_2_id' => $service_classification['level_2_id'],
                            'level_2_name' => $service_classification['level_2_name'],
                            'level_3_id' => $service_classification['level_3_id'],
                            'level_3_name' => $service_classification['level_3_name'],
                            'order_type' => 1,                                                                  //订单类型  1 订单2 洗车单
                        );
                        array_push($work_hour_report,$_work_hour_report_1);
                }
            }
        }
    }
    //洗车单
    $wash_os = CarWashOrder::where('status',1417)
        ->whereBetween('pay_time',array($begin_date,$end_date))
        // ->where('shop_id',$shop_id)
        ->get();
    foreach($wash_os as $wo){
        $shop=MaintenanceShops::find($wo->shop_id);
        $user=Users::find($wo->user_id);
        $pp = ProductPackage::find($wo->producd_id);
        $sc = Report::get_service_classification($pp->classify);
        $_work_hour_report_1 = array(
            'pay_time' => $wo->pay_time,                                    //支付时间
            'shop_name' => isset($shop)?$shop->name:"",                     //门店名字          
            'shop_id' => $wo->shop_id,                                      //门店id     
            'car_no' => $wo->car_no,                                        //车牌号    
            'user_name' => isset($user)?$user->real_name:"",                //用户名
            'user_type' => isset($user)&&$user->type==1?"个人":"企业",      //用户类型
            'car_model' => "",                                              //车型
            'car_model_level' => "",                                        //车辆级别
            'order_id' => $wo->order_id,                                    //订单号
            'product_name' => $pp->name,                                    //项目名字 
            'product_id' => $wo->product_id,                                //项目ID
            'product_type' => 0,                                            //0是标准 1是非标
            'product_amount' => $wo->amount,                                //原价
            'product_discount' => $wo->dis_amount,                          //优惠金额
            'counter_amount' => $wo->pay_amount,                            //实际折后价格
            'supply_amount' => 0,                                           //耗材成本
            'service_amount' => $wo->amount,                                //服务价格
            'level_1_id' => $sc['level_1_id'],                              //分类
            'level_1_name' => $sc['level_1_name'],          
            'level_2_id' => $sc['level_2_id'],
            'level_2_name' => $sc['level_2_name'],
            'level_3_id' => $sc['level_3_id'],
            'level_3_name' => $sc['level_3_name'],
            'order_type' => 2,                                              //订单类型  1 订单2 洗车单
        );
        array_push($work_hour_report,$_work_hour_report_1);
    }
    //计算这个时间段内 退货的 记录

    // $ro=ReturnOrder::leftJoin("orders","orders.order_id","=","return_order.order_id")
    //     ->where("return_order.status",1)->where("orders.shop_id",$shop_id)->where("orders.place",2)->whereBetween("return_order.implement_time",array($begin_date,$end_date))->select(array('return_order.id',"orders.*"))->get();
    // foreach ($ro as $_ro){
    //     $roi=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->where("type",3)->first();//说明是 均摊的退货
    //     if($roi){
    //         $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",3)->where("type",3)->get();
    //     }else{
    //         $ori=ReturnOrderItem::where("return_order_id",$_ro->id)->where("status",1)->get();//
    //     }
    //     $pacek_arr=array();
    //     foreach ($ori as $_roi) {
    //         if($_roi->type != 4) {
    //             switch ($_roi->product_type) {
    //                 case 'enterprise':
    //                     $order_product_item = OrderProductEsItem::find($_roi->order_product_item_id);
    //                     $op = OrderProductEsPackage::find($order_product_item->order_product_package_id);
    //                     $product = ProductPackage::find($op->package_id);
    //                     $product_name = $op->package_name;
    //                     $item_type = $order_product_item->type;
    //                     $st = $product->classify;
    //                     break;
    //                 case 'activity':
    //                     $order_product_item = OrderProductAcItem::find($_roi->order_product_item_id);
    //                     $op = OrderProductAcPackage::find($order_product_item->order_product_package_id);
    //                     $product = ProductPackage::find($op->package_id);
    //                     $product_name = $op->package_name;
    //                     $item_type = $order_product_item->type;
    //                     $st = $product->classify;
    //                     break;
    //                 case 'promotion':
    //                     break;
    //                 case 'base':
    //                     $order_product_item = OrderProductBaseItem::find($_roi->order_product_item_id);
    //                     $op = OrderProduct::find($order_product_item->order_product_package_id);
    //                     $product = ProductPackage::find($op->product_id);
    //                     $product_name = $op->package_name;
    //                     $item_type = $order_product_item->type;
    //                     $st = $product->classify;
    //                     break;
    //                 case 'non_standard':
    //                     $order_product_item = OrderProductNonStandard::find($_roi->order_product_item_id);
    //                     $op = OrderProduct::find($order_product_item->order_product_id);
    //                     $product_name = $op->package_name;
    //                     $item_type = $order_product_item->service_type;
    //                     $s = Service::find($op->product_id);
    //                     $st = $s->classify;
    //                     break;
    //                 case 'service_card':
    //                     $order_product_item = OrderProductScItem::find($_roi->order_product_item_id);
    //                     $op = OrderProduct::find($order_product_item->order_product_id);
    //                     $product = ProductPackage::find($op->product_id);
    //                     $product_name = $op->package_name;
    //                     $item_type = $order_product_item->type;
    //                     $st = $product->classify;
    //                     break;
    //             }
    //             $counter_amount=$supply_amount=$service_amount=0;
    //             $counter_amount=-$_roi->return_amount;
    //             switch ($_roi->type) {
    //                 case '0'://（退库存）退成本退钱
    //                     $before_return=MaintenanceReturnOrder::where("source",'return_order')->where("product_type",$_roi->product_type)->where("product_item_id",$_roi->order_product_item_id)->get();

    //                     foreach ($before_return as $_br) {
    //                         $temp_msi=MaintenanceStorageItem::where("return_order_id",$_br->id)->where("status",1)->get();
    //                         foreach ($temp_msi as $_ts) {
    //                             $temp_moi=MaintenanceOutItem::find($_ts->out_id);
    //                             $supply_amount=$supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
    //                         }
    //                     }
    //                     $supply_amount=-$supply_amount;
    //                     break;
    //                 case '1'://1配件
    //                     break;
    //                 case '2'://2工时
    //                     $service_amount=-$_roi->return_amount;
    //                     break;
    //                 case '3'://3均摊
    //                     if($item_type==2)//说明是工时
    //                         $service_amount=-$_roi->return_amount;
    //                     break;
    //             }
    //             if(isset($pacek_arr[$_roi->product_type."_".$op->id])){
    //                 $temp=$pacek_arr[$_roi->product_type."_".$op->id];
    //                 $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
    //                 $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
    //                 $temp["service_amount"]=$temp["service_amount"]+$service_amount;
    //                 $pacek_arr[$_roi->product_type."_".$op->id]=$temp;
    //             }else{
    //                 $pacek_arr[$_roi->product_type."_".$op->id]=array('product_name'=>$product_name,'product_id'=>$op->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
    //             }
    //         }else {
    //             $ops = OrderProduct::find($_roi->order_product_id);
    //             if ($ops->product_type == 'enterprise') {//说明是 企业报
    //                 $opep = OrderProductEsPackage::where("order_product_id", $_roi->order_product_id)->get();
    //                 foreach ($opep as $_opep) {
    //                     $product = ProductPackage::find($_opep->package_id);
    //                     $product_name = $_opep->package_name;
    //                     $st = $product->classify;
    //                     $item = OrderProductEsItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
    //                     foreach($item as $_it){
    //                         $item_type = $_it->type;
    //                         $counter_amount=$supply_amount=$service_amount=0;                                
    //                         if($item_type == 1) {
    //                             $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
    //                             foreach ($before_return as $_br) {
    //                                 $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
    //                                 foreach ($temp_msi as $_ts) {
    //                                     $temp_moi = MaintenanceOutItem::find($_ts->out_id);
    //                                     $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
    //                                 }
    //                             }
    //                             $supply_amount = -$supply_amount;
    //                             $counter_amount=-$_it->counter_amount*$_it->num;
    //                         }else {
    //                             $service_amount = -$_it->counter_amount;
    //                             $counter_amount=-$_it->counter_amount;
    //                         }
    //                         if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
    //                             $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
    //                             $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
    //                             $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
    //                             $temp["service_amount"]=$temp["service_amount"]+$service_amount;
    //                             $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
    //                         }else{
    //                             $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_id'=>$ops->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
    //                         }
    //                     }
    //                 }
    //             } elseif ($ops->product_type == 'activity') {//说明是 企业报
    //                 $opep = OrderProductAcPackage::where("order_product_id", $_roi->order_product_id)->get();
    //                 foreach ($opep as $_opep) {
    //                     $product = ProductPackage::find($_opep->package_id);
    //                     $product_name = $_opep->package_name;
    //                     $st = $product->classify;
    //                     $item = OrderProductAcItem::where("order_product_package_id", $_opep->id)->where('type', 2)->get();
    //                     foreach($item as $_it){
    //                         $item_type = $_it->type;
    //                         $counter_amount=$supply_amount=$service_amount=0;                                
    //                         if($item_type == 1) {
    //                             $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
    //                             foreach ($before_return as $_br) {
    //                                 $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
    //                                 foreach ($temp_msi as $_ts) {
    //                                     $temp_moi = MaintenanceOutItem::find($_ts->out_id);
    //                                     $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
    //                                 }
    //                             }
    //                             $supply_amount = -$supply_amount;
    //                             $counter_amount=-$_it->counter_amount*$_it->num;
    //                         }else {
    //                             $service_amount = -$_it->counter_amount;
    //                             $counter_amount=-$_it->counter_amount;
    //                         }
    //                         if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
    //                             $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
    //                             $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
    //                             $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
    //                             $temp["service_amount"]=$temp["service_amount"]+$service_amount;
    //                             $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
    //                         }else{
    //                             $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_id'=>$ops->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
    //                         }
    //                     }
    //                 }
    //             } elseif ($ops->product_type == 'base') {//说明是 基础
    //                 $item = OrderProductBaseItem::where("order_product_package_id", $ops->id)->get();
    //                 $product = ProductPackage::find($ops->product_id);
    //                 $product_name = $ops->product_name;
    //                 $st = $product->classify;
    //                 foreach($item as $_it){
    //                     $item_type = $_it->type;
    //                     $counter_amount=$supply_amount=$service_amount=0;                            
    //                     if($item_type == 1) {
    //                         $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
    //                         foreach ($before_return as $_br) {
    //                             $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
    //                             foreach ($temp_msi as $_ts) {
    //                                 $temp_moi = MaintenanceOutItem::find($_ts->out_id);
    //                                 $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
    //                             }
    //                         }
    //                         $supply_amount = -$supply_amount;
    //                         $counter_amount=-$_it->counter_amount*$_it->num;
    //                     }else {
    //                         $service_amount = -$_it->counter_amount;
    //                         $counter_amount=-$_it->counter_amount;
    //                     }
    //                     if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
    //                         $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
    //                         $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
    //                         $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
    //                         $temp["service_amount"]=$temp["service_amount"]+$service_amount;
    //                         $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
    //                     }else{
    //                         $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_id'=>$ops->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
    //                     }
    //                 }
    //             } elseif ($ops->product_type == 'non_standard') {//说明是 非标
    //                 $item = OrderProductNonStandard::where("order_product_id", $ops->id)->get();
    //                 $product_name = $ops->product_name;
    //                 $s = Service::find($ops->product_id);
    //                 $st = $s->classify;
    //                 foreach($item as $_it){
    //                     $item_type = $_it->service_type;
    //                     $counter_amount=$supply_amount=$service_amount=0;                            
    //                     if($item_type == 1) {
    //                         $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
    //                         foreach ($before_return as $_br) {
    //                             $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
    //                             foreach ($temp_msi as $_ts) {
    //                                 $temp_moi = MaintenanceOutItem::find($_ts->out_id);
    //                                 $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
    //                             }
    //                         }
    //                         $supply_amount = -$supply_amount;
    //                         $counter_amount=-$_it->counter_amount*$_it->num;
    //                     }else {
    //                         $service_amount = -$_it->counter_amount;
    //                         $counter_amount=-$_it->counter_amount;
    //                     }
    //                     if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
    //                         $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
    //                         $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
    //                         $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
    //                         $temp["service_amount"]=$temp["service_amount"]+$service_amount;
    //                         $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
    //                     }else{
    //                         $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_id'=>$ops->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
    //                     }
    //                 }
    //             } elseif ($ops->product_type == 'service_card') {//说明是 套餐卡
    //                 $item = OrderProductScItem::where("order_product_id", $ops->id)->get();
    //                 $product = ProductPackage::find($ops->product_id);
    //                 $product_name = $ops->product_name;
    //                 $st = $product->classify;
    //                 foreach($item as $_it){
    //                     $item_type = $_it->type;
    //                     $counter_amount=$supply_amount=$service_amount=0;                            
    //                     if($item_type == 1) {
    //                         $before_return = MaintenanceReturnOrder::where("source", 'return_order')->where("product_type", $_roi->product_type)->where("product_item_id", $_roi->order_product_item_id)->get();
    //                         foreach ($before_return as $_br) {
    //                             $temp_msi = MaintenanceStorageItem::where("return_order_id", $_br->id)->where("status", 1)->get();
    //                             foreach ($temp_msi as $_ts) {
    //                                 $temp_moi = MaintenanceOutItem::find($_ts->out_id);
    //                                 $supply_amount = $supply_amount+($_ts->number*$temp_moi->tax_purchase_price);
    //                             }
    //                         }
    //                         $supply_amount = -$supply_amount;
    //                         $counter_amount=-$_it->counter_amount*$_it->num;
    //                     }else {
    //                         $service_amount = -$_it->counter_amount;
    //                         $counter_amount=-$_it->counter_amount;
    //                     }
    //                     if(isset($pacek_arr[$_roi->product_type."_".$ops->id])){
    //                         $temp=$pacek_arr[$_roi->product_type."_".$ops->id];
    //                         $temp["counter_amount"]=$temp["counter_amount"]+$counter_amount;
    //                         $temp["supply_amount"]=$temp["supply_amount"]+$supply_amount;
    //                         $temp["service_amount"]=$temp["service_amount"]+$service_amount;
    //                         $pacek_arr[$_roi->product_type."_".$ops->id]=$temp;
    //                     }else{
    //                         $pacek_arr[$_roi->product_type."_".$ops->id]=array('product_name'=>$product_name,'product_id'=>$ops->id,'counter_amount'=>$counter_amount,'supply_amount'=>$supply_amount,'service_amount'=>$service_amount,"st"=>$st);
    //                     }
    //                 }
    //             }
    //         }
    //     }
    //     foreach ($pacek_arr as $_pa) {
    //         $service_classification = Report::get_service_classification($_pa["st"]);
    //         $_work_hour_report = array(
    //             'order_id' => $_ro->order_id, //派工单号
    //             'product_name' => $_pa['product_name'],   //客户名称
    //             'product_id' => $_pa['product_id'],   //客户名称
    //             'product_type' =>0,   //服务包是标准还是非标  0是标准 1是非标
    //             'product_amount' => 0,
    //             'product_discount' => 0,
    //             'counter_amount' => $_pa['counter_amount'],
    //             'supply_amount' => $_pa['supply_amount'],
    //             'service_amount' => $_pa['service_amount'],
    //             'level_1_id' => $service_classification['level_1_id'],
    //             'level_1_name' => $service_classification['level_1_name'],
    //             'level_2_id' => $service_classification['level_2_id'],
    //             'level_2_name' => $service_classification['level_2_name'],
    //             'level_3_id' => $service_classification['level_3_id'],
    //             'level_3_name' => $service_classification['level_3_name'],
    //             'reteurn_type' => 1,
    //         );
    //         array_push($work_hour_report,$_work_hour_report);
    //     }
    // }
    //洗车单 退货
    // $wash_os_r = ReturnCarwashOrder::leftJoin("car_wash_order","car_wash_order.order_id","=","return_carwash_order.order_id")
    //     ->where('return_carwash_order.status',1)
    //     ->whereBetween('return_carwash_order.implement_time',array($begin_date,$end_date))
    //     ->where('car_wash_order.shop_id',$shop_id)
    //     ->select(array('return_carwash_order.id',"car_wash_order.*"))
    //     ->get();

    // foreach($wash_os_r as $wor){
    //     $pp = ProductPackage::find($wor->producd_id);
    //     $sc = Report::get_service_classification($pp->classify);
    //     $_work_hour_report_2 = array(
    //         'order_id' => $wor->order_id, //派工单号
    //         'product_name' => $pp->name,   //客户名称
    //         'product_id' => $wor->product_id,   //客户名称
    //         'product_type' => 0, //  0是标准 1是非标
    //         'product_amount' => 0,
    //         'product_discount' => 0,
    //         'counter_amount' => -$wor->pay_amount,
    //         'supply_amount' => 0,
    //         'service_amount' => -$wor->amount,
    //         'level_1_id' => $sc['level_1_id'],
    //         'level_1_name' => $sc['level_1_name'],
    //         'level_2_id' => $sc['level_2_id'],
    //         'level_2_name' => $sc['level_2_name'],
    //         'level_3_id' => $sc['level_3_id'],
    //         'level_3_name' => $sc['level_3_name'],
    //         'reteurn_type' => 1,
    //     );
    //     array_push($work_hour_report,$_work_hour_report_2);
    // }
    
    return $work_hour_report;
}



